package com.kfm.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet({"/hello"})
public class HelloServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("HelloServlet doGet()...");

        // 设置编码
        request.setCharacterEncoding("utf-8");

        // 获取一下请求头, 做 UA 验证
        String ua = request.getHeader("User-Agent");
        System.out.println("ua: " + ua);
        if (ua == null) {
            resp.getWriter().println("invalid request");
        } else {
            resp.getWriter().println("welcome");
        }

        // 获取请求参数
        String queryString = request.getQueryString(); // 请求参数, 解析一下字符串
        String name = request.getParameter("name");// 获取指定参数
        String age = request.getParameter("age"); // 获取指定参数
        System.out.println("queryString: " + queryString);
        System.out.println("name: " + name);
        System.out.println("age: " + age);
    }
}

